package com.ys.fmms.controller;

import com.ys.fmms.pojo.Permission;
import com.ys.fmms.service.PermissionService;
import com.ys.fmms.util.JwtHelper;
import com.ys.fmms.util.Result;
import com.ys.fmms.util.TimeUtils;
import io.swagger.annotations.Api;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@Api(tags = "菜单按钮权限表")
@RequestMapping("/fmms/permissionController")
public class PermissionController {

    @Resource
    private PermissionService permissionService;

    @Operation(summary = "查询所有菜单，返回的是树形结构的菜单数组")
    @GetMapping("/getAllMenuList")
    public Result getAllMenuList() {
        List<Permission> permissionList = permissionService.getAllMenuList();
        return Result.ok(permissionList);
    }

    @Operation(summary = "递归删除菜单")
    @DeleteMapping("/remove/{id}")
    public Result removeMenuList(@PathVariable String id) {
        permissionService.removeChildById(id);
        return Result.ok().message("删除成功");
    }

    @Operation(summary = "给角色分配权限")
    @PostMapping("/doAssign")
    public Result doAssign(
            @Parameter(description = "角色Id") String roleId,
            @RequestBody
            @Parameter(in = ParameterIn.QUERY, description = "菜单权限数组id") String[] permissionId) {
        permissionService.saveRolePermissionRelationship(roleId, permissionId);
        return Result.ok();
    }

    @Operation(summary = "根据角色Id获取对应的菜单列表")
    @GetMapping("/getMenuList/{roleId}")
    public Result getMenuListByRoleId(@PathVariable String roleId) {
        Map<String, Object> map = new HashMap<>();
        List<Permission> rolePermission = permissionService.getMenuListByRoleId(roleId);
        map.put("childrenMenuList", rolePermission);
        return Result.ok(map);
    }

    @Operation(summary = "增加菜单")
    @PostMapping("/addMenuList")
    public Result addMenuList(@RequestBody Permission permission) {
        permission.setCreateTime(TimeUtils.currentDate());
        permissionService.save(permission);
        return Result.ok();
    }

    @Operation(summary = "修改菜单")
    @PutMapping("/updateMenuList")
    public Result updateMenuList(@RequestBody Permission permission) {
        permission.setModifyTime(TimeUtils.currentDate());
        permissionService.updateById(permission);
        return Result.ok();
    }

    @Operation(summary = "当前登陆用户菜单和按钮列表")
    @GetMapping("/getMenuListByUser")
    public Result getPermissionListByUser(@RequestHeader String token) {
        String token1 = token;
        Map<String, Object> map = permissionService.getUserMenuAndButtonList(token);
        return Result.ok(map);
    }
}
